//
//  LoginViewController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/13.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "LoginViewController.h"
#import "PhoneFieldView.h"
#import "ShareItemView.h"
#import "VerificationCodeLoginVC.h"
#import "EmailLoginLoginVC.h"
#import "RetrievePasswordController.h"
#import "BindingPhoneController.h"

@interface LoginViewController ()
/**手机号输入框*/
@property (nonatomic,strong)PhoneFieldView*phoneView;
/**密码输入框*/
@property (nonatomic,strong)PhoneFieldView*paswView;
/**协议点击文字*/
@property (nonatomic,strong)AgreementView*agreementView;
/**登录按钮*/
@property (nonatomic,strong)UIButton*loginBtn;
/**底部分享按钮视图*/
@property (nonatomic,strong)ShareItemView*shareItemView;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    [self setNotificaAction];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavBarBackgroundAndClearColor];
}
-(void)setNotificaAction{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chaneTextFiel) name:UITextFieldTextDidChangeNotification object:nil];
}
-(void)chaneTextFiel{
    if ([self.phoneView.textFile.text checkPhone] && [self.paswView.textFile.text chekePaswd] && self.agreementView.isSelected) {
        self.loginBtn.enabled = YES;
    }else{
        self.loginBtn.enabled = NO;
    }
}
/**
 跳转到找回密码页面
 */
-(void)pushRetrievePasswordViewAction{
    [self.navigationController pushViewController:[RetrievePasswordController new] animated:YES];
}
/**
 跳转验证码登录页面
 */
-(void)pushCodeViewAction{
    [self.navigationController pushViewController:[VerificationCodeLoginVC new] animated:YES];
}

/**
 跳转邮箱登录页面
 */
-(void)pushEmailViewAction{
    [self.navigationController pushViewController:[EmailLoginLoginVC new] animated:YES];

}
-(void)createUI{
    UIImageView * topImage = [[UIImageView alloc] initWithImage:IMAGE_NAMED(@"login_icon")];
    
    UILabel * label = UILabel.new;
    label.text = @"忘记密码";
    label.font = SYSTEMFONT(12);
    label.textColor = UIColorFromRGB(0x273323);
    label.userInteractionEnabled = YES;
    [label addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushRetrievePasswordViewAction)]];
    
    UILabel *codeLogin = UILabel.new;
    codeLogin.textColor = UIColorFromRGB(0x24B34A);
    codeLogin.font = SYSTEMFONT(15);
    codeLogin.text = @"验证码登录";
    codeLogin.userInteractionEnabled =YES;
    [codeLogin addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushCodeViewAction)]];
    
    
    UILabel *emailLogin = UILabel.new;
    emailLogin.textColor = UIColorFromRGB(0x24B34A);
    emailLogin.font = SYSTEMFONT(15);
    emailLogin.text = @"邮箱登录";
    emailLogin.userInteractionEnabled =YES;
    [emailLogin addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pushEmailViewAction)]];

    
    [self.view addSubview:topImage];
    [self.view addSubview:self.phoneView];
    [self.view addSubview:self.paswView];
    [self.view addSubview:label];
    
    [self.view addSubview:self.agreementView];
    [self.view addSubview:self.loginBtn];
    
    [self.view addSubview:codeLogin];
    [self.view addSubview:emailLogin];
    [self.view addSubview:self.shareItemView];
    
    [topImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.top.mas_equalTo(88);
        make.width.height.mas_equalTo(80);
    }];
    
    [self.phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(topImage.mas_bottom).offset(45);
        make.height.mas_equalTo(40);
    }];
    [self.paswView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(self.phoneView.mas_bottom).offset(10);
        make.height.mas_equalTo(40);
    }];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.paswView);
        make.top.mas_equalTo(self.paswView.mas_bottom).offset(10);
    }];
    [self.agreementView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(55);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(label.mas_bottom).offset(35);
    }];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.top.mas_equalTo(self.agreementView.mas_bottom).offset(15);
        make.height.mas_equalTo(40);
    }];
    
    [codeLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.loginBtn);
        make.top.mas_equalTo(self.loginBtn.mas_bottom).offset(20);
    }];
    [emailLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.loginBtn);
        make.centerY.mas_equalTo(codeLogin);
    }];
    [self.shareItemView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        make.bottom.mas_equalTo(self.view.mas_bottom).offset(-60);
    }];

}
#pragma mark ---懒加载

-(PhoneFieldView*)phoneView{
    if(!_phoneView){
        _phoneView=[[PhoneFieldView alloc] initWithFrame:CGRectZero];
        _phoneView.textFile.keyboardType = UIKeyboardTypeNumberPad;
        
    }
    return _phoneView;
}

-(PhoneFieldView*)paswView{
    if(!_paswView){
        _paswView=[[PhoneFieldView alloc] initWithFrame:CGRectZero];
        _paswView.leftName.text = @"密码";
        _paswView.textFile.placeholder = @"输入密码";
    }
    return _paswView;
}
-(AgreementView*)agreementView{
    if(!_agreementView){
        _agreementView=[[AgreementView alloc] initWithFrame:CGRectZero];
        kWeakSelf(weakSelf);
        _agreementView.touchHandler = ^{
            [weakSelf chaneTextFiel];
        };
    }
    return _agreementView;
}

-(UIButton*)loginBtn{
    if(!_loginBtn){
        _loginBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        [_loginBtn setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x9ADCAB)] forState:UIControlStateDisabled];
        [_loginBtn setBackgroundImage:[UIImage createImageWithColor:UIColorFromRGB(0x24B34A)] forState:UIControlStateNormal];
        _loginBtn.enabled = NO;
        kWeakSelf(weakSelf);
        [_loginBtn addActionHandler:^{
            if ([weakSelf.phoneView.textFile.text checkPhone]) {
                [UserModelManager goToLogin];
            }
        }];
        
        
        
    }
    return _loginBtn;
}
-(ShareItemView*)shareItemView{
    if(!_shareItemView){
        _shareItemView=[[ShareItemView alloc] initWithFrame:CGRectZero];
        kWeakSelf(weakSelf);
        _shareItemView.tapBlock = ^(NSInteger index) {
            
            [weakSelf.navigationController pushViewController:[BindingPhoneController new] animated:YES];
        };
    }
    return _shareItemView;
}

@end
